package com.example.yunhailutong.zhongtan.net.base;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.json.JSONObject;

/**
 * 这个类是处理请求的出错误处理
 */
public abstract class ErrorListener implements Response.ErrorListener {

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        String msg = "错误未知";
        try {
            switch (volleyError.networkResponse.statusCode) {
                case 400:
                    try {
                        JSONObject jsonObject = new JSONObject(new String(volleyError.networkResponse.data, "utf-8"));
                        msg = jsonObject.getString("description");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
//                case 401:
//                    msg = "token无效2";
//                    break;
                case 503:
                    msg = "请求接口超过调用频率限制, 即 接口被限流.";
                    break;
            }
            onError(msg);
        } catch (Exception e) {
        }
    }

    protected abstract void onError(String error);

}
